<?php
namespace app\modules\v1\services\validate;

use app\modules\common\components\Common;
use app\modules\v1\verify\LoginVerify;
use Exception;

class LoginValidate
{
    public function doInfo($initData)
    {
        try {
            $verify = new LoginVerify();
            $verify->scenario = 'doinfo';
            $verify->userId = $initData['userId'] ?? "";
            if ($verify->validate()) {
                return Common::codeReturn();
            } else {
                $messageArr = Common::errorMessage($verify);
                throw new Exception($messageArr['desc'], $messageArr['code']);
            }
        } catch (Exception $exception) {
            return Common::codeReturn([], $exception->getCode(), $exception->getMessage());
        }
    }

    public function doLog($initData)
    {
        try {
            $verify = new LoginVerify();
            $verify->scenario = 'dolog';
            $verify->username = $initData['username'] ?? "";
            $verify->password = $initData['password'] ?? "";
            if ($verify->validate()) {
                return Common::codeReturn();
            } else {
                $messageArr = Common::errorMessage($verify);
                throw new Exception($messageArr['desc'], $messageArr['code']);
            }
        } catch (Exception $exception) {
            return Common::codeReturn([], $exception->getCode(), $exception->getMessage());
        }
    }
}
